package com.addplus.townmall.server.web.consumer.action;

import com.addplus.townmall.server.api.constant.ErrorCode;
import com.addplus.townmall.server.api.model.base.ReturnDataSet;
import com.addplus.townmall.server.api.service.queue.GiftCardBatchService;
import com.addplus.townmall.server.web.consumer.container.AddplusContainer;
import com.addplus.townmall.server.web.consumer.utils.WebUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;

@RestController
public class CommonAction {

    @Autowired
    private AddplusContainer addplusContainer;


    @GetMapping(value = "/get/{model}/{module}/{service}/{method}")
    public Object get(@PathVariable String model, @PathVariable String module, @PathVariable String service, @PathVariable String method, HttpServletRequest request) throws Exception {
        ReturnDataSet returnDataSet=addplusContainer.invoke(model+"."+module+"."+service,method,request.getParameterMap(),true);
        return returnDataSet;
    }

    @PostMapping(value = "/post/{model}/{module}/{service}/{method}",produces = MediaType.APPLICATION_JSON_VALUE + ";charset=UTF-8")
    public Object post(@PathVariable String model, @PathVariable String module, @PathVariable String service, @PathVariable String method, HttpServletRequest request) throws Exception {
        InputStream is;
        String contentStr;
        Map param = new HashMap();
        ReturnDataSet returnDataSet=null;
        try
        {
            is = request.getInputStream();
            contentStr = WebUtil.inputStreamString(is);
            param = addplusContainer.stringToMap(model+"."+module+"."+service,method,contentStr,false);
        }
        catch (Exception e)
        {
            returnDataSet = new ReturnDataSet();
            returnDataSet.setReturnCode(ErrorCode.SYS_ERROR_SERVICE.getCode());
            returnDataSet.setErrorInfo(e.getMessage());
            return returnDataSet;
        }
        return addplusContainer.invoke(model+"."+module+"."+service,method,param,false);
    }

}
